<?php
/**
* PHP Template.
*/
class EventsModel extends Model {
//private $user = "calendar@wahomebrewers.org";
//private $pass = "calendar-waha";
    private $calList = null;
    public function __construct() {
            parent::Model();
            $this->load->library("errors");
            $this->load->model("LocationsModel");
            $this->load->model("ContactModel");
        }
        /**
        *	Function returns ID for calendar to be used by setUser
        */
    private function getCalendarIdByName($name) {
            $cal = $name; // "default" by default
            if($this->getCalendarList()==null) {
                // have an error...escape out?
                    return $name;
                }
            return $name;
        }
    public function getSingleEvent($eventId) {
            $query = "SELECT event_id,name,short_description,long_description,event_start,event_end,location_id,contact_id,web_link FROM events WHERE event_id=?";
            $retVal = array();
            $res = $this->db->query($query,array($eventId));
            if($res->num_rows()>0) {
                    $tmp = explode(" ",$res->row()->event_start);
                    $startDate = $tmp[0];
                    $startTime = $tmp[1];
                    $tmp = explode(" ",$res->row()->event_start);
                    $endDate = $tmp[0];
                    $endTime = $tmp[1];
                    $locationArr = $this->LocationsModel->getLocation($res->row()->location_id);
                    $contactArr = $this->ContactModel->getContact($res->row()->contact_id);
                    array_push($retVal,
                        array(
                        "id"=>$res->row()->event_id,
                        "title"=>$res->row()->name,
                        "startDate"=>$startDate,
                        "startTime"=>$startTime,
                        "endDate"=>$endDate,
                        "endTime"=>$endTime,
                        "short_description"=>$res->row()->short_description,
                        "long_description"=>$res->row()->long_description,
                        "locArr"=>$locationArr,
                        "contactArr"=>$contactArr,
                        "web"=>$res->row()->web_link)
                        );
                }
            return $retVal;
        }
    private function fillCalendarFeedList() {
        
        }
    private function setCalendarList($calList){
            $this->calList = $calList;
        }
    private function getCalendarList(){
            return null;
        }
        /**************************************** PUBLIC FUNCTIONS **************************************/
    public function createEvent($title,$startDate,$endDate,$desc="",$ldesc="",$web="",$who="",$where="") {
            $insertArrValues = array(
                "name"=>$title,
                "short_description"=>$desc,
                "long_description"=>$ldesc,
                "event_start"=>$startDate,
                "event_end"=>$endDate,
                "web_link"=>$web
                );
            $query = $this->db->insert_string('events', $insertArrValues);
            $createdEntry = $this->db->query($query);
            return ($createdEntry==1); // RETURN id
        }
    public function updateEvent($eventId,$title,$startDate,$endDate,$desc="",$ldesc="",$web="",$who="",$where="") {
            $updateArrValues = array(
                "name"=>$title,
                "short_description"=>$desc,
                "long_description"=>$ldesc,
                "event_start"=>$startDate,
                "event_end"=>$endDate,
                "web_link"=>$web
                );
            $where = "event_id = " . $eventId;
            $query = $this->db->update_string('events',$updateArrValues,$where);
            $createdEntry = $this->db->query($query);
            return ($createdEntry==1); // RETURN success
        }
    public function deleteEvent($eventId) {
            $query = "DELETE FROM events WHERE event_id=? LIMIT 1";
            $createdEntry = $this->db->query($query,array($eventId));
            return ($createdEntry==1); // RETURN success
        }
        /**
        * Gets specific event by ID
        *
        * @param unknown_type $id
        * @return unknown
        */
    function getEventById($id) {
            $query = "SELECT event_id,name,event_start,event_end,short_description,long_description,web_link FROM events WHERE event_id=? ORDER BY event_start ASC LIMIT 1";
            $retVal = array();
            $res = $this->db->query($query,array($id));
            if($res->num_rows()>0) {
                    foreach ($res->result() as $row)
                        {
                            $tmp = explode(" ",$row->event_start); // datetime is split by space
                            $startDate = $tmp[0];
                            $startTime = $tmp[1];
                            $tmp = explode(" ",$row->event_start);
                            $endDate = $tmp[0];
                            $endTime = $tmp[1];
                            array_push($retVal,
                                array(
                                "id"=>$row->event_id,
                                "title"=>$row->name,
                                "startDate"=>$startDate,
                                "startTime"=>$startTime,
                                "endDate"=>$endDate,
                                "endTime"=>$endTime,
                                "short_description"=>$row->short_description,
                                "long_description"=>$row->long_description,
                                "web_link"=>$row->web_link
                                )
                                );
                        }
                }
            return $retVal;
        }
        /**
        * Gets Events by Keyword search
        *
        * @param unknown_type $calUserName
        * @param unknown_type $keyword
        * @return unknown
        */
    public function getEventsByKeyword($calUserName,$keyword="default") {
            return "";
        }
        /**
        * Returns events list by date
        *
        * @param unknown_type $calUserName
        * @param unknown_type $start
        * @param unknown_type $end
        * @return unknown
        */
    public function getEventsList($calUserName,$start,$end,$maxCount=null,$groupBy="") {
            $groupQuery = (($groupBy)?" GROUP BY " . $groupBy:"");
            $limitQuery = (($maxCount)?" LIMIT " . $maxCount:"");
        
            $query = "SELECT event_id,name,event_start,event_end,short_description FROM events WHERE event_start BETWEEN '" . $start . "' AND '" . $end . "' " . $groupQuery . " ORDER BY event_start ASC" . $limitQuery;
            $retVal = array();
            $res = $this->db->query($query);
            if($res->num_rows()>0) {
                    foreach ($res->result() as $row)
                        {
                            $tmp = explode(" ",$row->event_start);
                            $startDate = $tmp[0];
                            $startTime = $tmp[1];
                            $tmp = explode(" ",$row->event_start);
                            $endDate = $tmp[0];
                            $endTime = $tmp[1];
                            array_push($retVal,
                                array(
                                "id"=>$row->event_id,
                                "title"=>$row->name,
                                "startDate"=>$startDate,
                                "startTime"=>$startTime,
                                "endDate"=>$endDate,
                                "endTime"=>$endDate,
                                "short_description"=>$row->short_description
                                )
                                );
                        
                        }
                }
            return $retVal;
        }
    public function getPublicCalendarList() {
            if($this->getCalendarList()==null)
                    $this->fillCalendarFeedList();
            return $this->getCalendarList();
        }
}
?>